1729C - Jumping on Tiles - CodeForces Solution


constructive algorithms strings

Please click on ads to support us..

Python Code:

import math
from sys import stdin
input = stdin.readline

t = int(input())
alpha = "abcdefghijklmnopqrstuvwxyz"
out = []
for _ in range(t):

  
  s = input().strip()
  p1 = s[0]
  p2 = s[-1]
  new = []
  cost = 0
  cost = (alpha.index(s[0]) - alpha.index(s[-1]))
  for i in range(len(s)):

    new.append([s[i],i+1])
  if cost <= 0:
    new.sort(key = lambda x: (x[0],x[1]))
  else:
    new.sort(key = lambda x: (x[0],-x[1]))

 
  end = len(s)

  seq = []

  start = False
  
  for i in range(len(new)):
    
    if (new[i][1] == 1 or new[i][1] == end) and start == False:

      start = True

    elif (new[i][1] == 1 or new[i][1] == end) and start == True:

      break

    elif start == True:

      seq.append(new[i][1])

  if cost > 0:
    seq.reverse()
  cost = abs(cost)
  seq.insert(0,1)
  seq.append(end)
  
  out.append(f'{cost} {len(seq)}')
  out.append(' '.join(map(str, seq)))
print('\n'.join(out))
      

C++ Code:

#include <bits/stdc++.h>

using namespace std;


void solve() {
	string s;
	cin >> s;
	int n = s.size();
	vector<vector<int>> pos(300);
	for (int i = 0; i < n; i++) pos[s[i]].push_back(i);
	vector<int> ans;
	if (s[0] <= s[n - 1]) {
		for (int i = s[0]; i <= s[n - 1]; i++) {
			for (int j : pos[i]) ans.push_back(j);
		}
	} 
	else {
		for (int i = s[0]; i >= s[n - 1]; i--) {
			for (int j : pos[i]) ans.push_back(j);
		}
	}
	cout << abs(s[0] - s[n - 1]) << " " << ans.size() << '\n';
	for (int i : ans) cout << i + 1 << " ";
	cout << '\n';
}
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int t;
	cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

1656B - Subtract Operation
1656A - Good Pairs
1367A - Short Substrings
87A - Trains
664A - Complicated GCD
1635D - Infinite Set
1462A - Favorite Sequence
1445B - Elimination
1656C - Make Equal With Mod
567A - Lineland Mail
1553A - Digits Sum
1359B - New Theatre Square
766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase
1656D - K-good
1426A - Floor Number
876A - Trip For Meal
1326B - Maximums
1635C - Differential Sorting
961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two